package com.example.springdata_repositories.controller;

import com.example.springdata_repositories.domain.Students;
import com.example.springdata_repositories.json.R;
import com.example.springdata_repositories.repository.StudentsRepository;
import com.example.springdata_repositories.service.StudentsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

@RestController
@RequestMapping("students")
public class StudentsController {

    @Resource
    private StudentsService studentsService;


    //@PostMapping("/{id}")
    @PostMapping("/")
    public R saveStudents(@RequestBody Students students) {
        try {
            Students s = studentsService.saveStudents(students);
            return R.success("保存学生资料成功！", s);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }


    @GetMapping("/{sid}")
    public R queryStudentBySid(@PathVariable("sid") String sid) {
        try {
            Students s = studentsService.queryStudentsBySid(sid);
            if (s != null) {
                return R.success("查询学生资料成功！", s);
            }
            return R.fail("查询学生资料失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }
}
